package ${domain}.module.wsys.mgr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.auth.Token;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.UserRole;
import ${domain}.module.wsys.mpr.UserRoleMapper;
import ${domain}.module.wsys.req.UserRoleCreateRequest;
import ${domain}.module.wsys.req.UserRoleDeleteRequest;
import ${domain}.module.wsys.req.UserRoleFindRequest;
import ${domain}.module.wsys.req.UserRoleUpdateRequest;
import ${domain}.module.wsys.rsp.UserRoleCreateResponse;
import ${domain}.module.wsys.rsp.UserRoleDeleteResponse;
import ${domain}.module.wsys.rsp.UserRoleFindResponse;
import ${domain}.module.wsys.rsp.UserRoleUpdateResponse;

/**
 * USER_ROLE - 用户角色授权
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class UserRoleManagerImpl implements UserRoleManager {

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public UserRoleCreateResponse create(UserRoleCreateRequest request, Token token) {
        UserRoleCreateResponse response = new UserRoleCreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long id = IDgenerator.nextId();
        UserRole entity = MapperUtil.map(request, UserRole.class);
        entity.setId(id);

        long result = userRoleMapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        response.setId(id);

        return response;
    }

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public UserRoleDeleteResponse delete(UserRoleDeleteRequest request, Token token) {
        UserRoleDeleteResponse response = new UserRoleDeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = userRoleMapper.delete(request.getId(), token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 更新
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public UserRoleUpdateResponse update(UserRoleUpdateRequest request, Token token) {
        UserRoleUpdateResponse response = new UserRoleUpdateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        UserRole entity = userRoleMapper.getById(request.getId(), token);
        if (entity == null) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
            return response;
        }

        MapperUtil.map(request, entity);
        long result = userRoleMapper.update(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.UPDATE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public UserRoleFindResponse find(UserRoleFindRequest request, Token token) {
        UserRoleFindResponse response = new UserRoleFindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<UserRole> pageInfo = new PageInfo<>(userRoleMapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }
}
